package com.bigflexlib.x.flash.geom
{
	
	
	import com.bigflexlib.x.flash.geom.Point.PolarPoint;
	
	import flash.display.DisplayObject;
	import flash.geom.Point;
	
	
	
	public class GeomUtil
	{


		

		/**
		 * 
		 * 
		 * We assume that the points are both in the same coordinate space
		 * 
		 * 
		 * @param rotation
		 * 			rotation in radians
		 * 
		 * @param scale
		 * 			scale
		 * 
		 * @param point1
		 * 			Coordinates of the anchor point before applying scaling and rotation
		 * 			IN THE INITIAL COORDINATE SPACE
		 * 
		 * @param point2
		 * 			Coordinates of the anchor point after applying scaling and rotation
		 * 			IN THE INITIAL COORDINATE SPACE
		 * 
		 * @return
		 * 			The position of the new origin of the system so that the anchors match after
		 * 			the required transformations
		 * 			IN THE INITIAL COORDINATE SPACE
		 * 
		 */
		public static function getTranslation( rotation:Number, scale:Number, point1:Point, point2:Point ):Point
		{

			var newOrigin:Point = getNewOriginAfterRegularTransformationAroundFixedAnchor( rotation, scale, point1 );
			
			var diff:Point = point1.subtract( point2 );
			
			return newOrigin.subtract( diff );			
		
		}




		/**
		 * 
		 * Note that all coords ( anchor and the returned point ) are in the initial coordinate space.
		 * This is a workaround to the lack of "registration point" in Flex 3.
		 * <pre>
		 * 
		 * 
		 * 
		 * </pre>
		 * 
		 * @param rotation
		 * @param scale
		 * @param anchor
		 * @return 
		 * 
		 */		
		public static function getNewOriginAfterRegularTransformationAroundFixedAnchor( rotation:Number, scale:Number, anchor:Point ):Point
		{
		
			// we transform to polar because we will need the distance to origin and the angle
			var polarAnchor:PolarPoint = PolarPoint.fromPoint( anchor );
			
			
			var newPolarAnchor:PolarPoint = new PolarPoint( 
													polarAnchor.len * scale , 
													polarAnchor.angle + rotation
													);

			return new Point( 
				polarAnchor.x - newPolarAnchor.x ,
				polarAnchor.y - newPolarAnchor.y
				);
				
		}




		public static function degreesToRadians( deg:Number ):Number
		{
			return  2 * Math.PI * ( deg / 360 );
		}
		
		
		
		
		
		public static function radiansToDegrees( rad:Number ):Number
		{
			return ( 360 * rad / ( Math.PI * 2 ) );
		}




		/**
		 * 
		 * Calculates the sum of the rotations from the target all the way up to root.
		 * If no root is passed, then it will go all the way up to the last parent in the chain.
		 * 
		 * @param target
		 * @param root
		 * @deprecated use matrix decomposition instead
		 * @return 
		 * 
		 * 
		 */
		public static function getTotalRotation( target:DisplayObject, root:DisplayObject ):int
		{
		
			var v:int = target.rotation;
			
			if ( target == root )
				return v;
			
			while ( target = target.parent )
			{
				v += target.rotation;			
				if ( target == root )
					break;
			}
						
						
			return v;
		
		}




	}
	
}